
package widgets
{


import mx.collections.ArrayCollection;
import mx.controls.CalendarLayout;
import mx.controls.DateChooser;
import mx.core.UITextField;
import mx.events.CollectionEvent;
import mx.events.DateChooserEvent;
import mx.events.FlexEvent;
	
// based on a component by www.cyberslingers.com:
// http://www.cyberslingers.com/weblog/post/Adding-Calendar-Event-Entries-to-the-Flex-DateChooser-Component.aspx

// extended to work with flex framework by simon gladman | http://flexmonkey.blogspot.com | June 2010

public class ExtendedDateChooser extends DateChooser
{
	public var narrativeField : String = 'narrative';
	public var dateField : String = "date";
	public var colorField : String = "color";
	
	private var _dateCollection:ArrayCollection;
	
	public function ExtendedDateChooser()
	{
		super();
		this.showToday=false;
		this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
		this.addEventListener(DateChooserEvent.SCROLL, onScroll); 
	}

	public function set dateCollection(value:ArrayCollection):void
	{
		this._dateCollection = value;
		this._dateCollection.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange);
		invalidateDisplayList();
	}

	private function onCollectionChange(event:CollectionEvent):void
	{
		invalidateDisplayList();
	}
	
	public function get dateCollection():ArrayCollection
	{
		return this._dateCollection;
	}
	
	private function onScroll(event:DateChooserEvent):void
	{
		invalidateDisplayList();
	}

	private function onCreationComplete(event:FlexEvent):void
	{
		invalidateDisplayList();
	}
	
	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
	{
		super.updateDisplayList(unscaledWidth, unscaledHeight);
		
		addEvents();
	}
	
	
	private function addEvents():void
	{
		if (dateCollection == null)
		{
			return;
		}
		
		// loop through all the calendar children
		for(var i:uint = 0; i < this.numChildren; i++)
		{
			var calendarObj:Object = this.getChildAt(i);
			
			// find the CalendarLayout object
			if(calendarObj.hasOwnProperty("className"))
			{
				if(calendarObj.className == "CalendarLayout")
				{
					var cal:CalendarLayout = CalendarLayout(calendarObj);
					
					// loop through all the CalendarLayout children
					for(var j:uint = 0; j < cal.numChildren; j++)
					{
						var dateLabel:Object = cal.getChildAt(j);
						
						// find all UITextFields
						if(dateLabel.hasOwnProperty("text"))
						{
							var day:UITextField = UITextField(dateLabel);

							var thisDate:Date = new Date(this.displayedYear, this.displayedMonth, parseInt(dateLabel.text));
							
							var eventArray:Array = dateHelper(thisDate);
							
							if(eventArray.length > 0)
							{
								var color : uint = eventArray[0][colorField];
								var lume : uint = 0.3* ((color & 0xff0000) >> 16) + 0.59* ((color & 0x00ff00) >> 8) + 0.11* (color & 0x0000ff);

								day.background = true;
								day.backgroundColor = color;
								
								day.setColor(lume < 127 ? 0xFFFFFF : 0x000000); 
								day.toolTip = eventArray[0][narrativeField];	
							}
							else
							{
								day.background = false;
								day.setColor(0x000000); 
								day.toolTip = '';
							}
						}
					}
				}
			}
		}
	}
	
	
	//--------------------------------------
	//  Helpers
	//--------------------------------------
	/**
	 * Build array of events for current date
	 * @param string - current date
	 * 
	 */ 
	private function dateHelper(date:Date):Array
	{
		var result:Array = new Array();
		for(var i:uint = 0; i < dateCollection.length; i++)
		{	
		
			if((dateCollection[i][dateField] as Date).toDateString() == date.toDateString())
			//if(dateCollection[i][dateField] as Date == date.toDateString())
			{ 
				result.push(dateCollection[i]);
			}
		}
		return result;
	}
}
}
